﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data; 
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WorkPlatm_CM_EBank2RJZ.model;
using wxt;

namespace WorkPlatm_CM_EBank2RJZ.views.dialogs
{
    /// <summary>
    /// Interaction logic for Dialog_cm_SelectPZH.xaml
    /// </summary>
    public partial class Dialog_cm_SelectPZH : Window
    {
        private List<Pzxx> list_pzxx = new List<Pzxx>() ; 
        public Dialog_cm_SelectPZH()
        {
            InitializeComponent(); 
            //初始化会计期间
            String year = LoginInfo.getInstance().Dywrq.Year.ToString();
            this.comb_kjqj.Items.Add(year + "01");
            this.comb_kjqj.Items.Add(year + "02");
            this.comb_kjqj.Items.Add(year + "03");
            this.comb_kjqj.Items.Add(year + "04");
            this.comb_kjqj.Items.Add(year + "05");
            this.comb_kjqj.Items.Add(year + "06");
            this.comb_kjqj.Items.Add(year + "07");
            this.comb_kjqj.Items.Add(year + "08");
            this.comb_kjqj.Items.Add(year + "09");
            this.comb_kjqj.Items.Add(year + "10");
            this.comb_kjqj.Items.Add(year + "11");
            this.comb_kjqj.Items.Add(year + "12");
            this.comb_kjqj.Items.Add(year + "13");
            this.comb_kjqj.SelectedIndex = LoginInfo.getInstance().Dywrq.Month - 1;
            getPzh();
            this.DataContext = this;
        }
        public List<Pzxx> List_pzxx {
            get { return this.list_pzxx; }
            set { this.list_pzxx = value; }
        }
        private void getPzh() {
            this.List_pzxx.Clear();
            String kjqj = "";
            try
            {
                kjqj = (this.comb_kjqj.SelectedValue.ToString());
            }
            catch (Exception e) {
               // MessageBox.Show(e.Message);
            } 
            String result = ""; 
            //初始化凭证号
            String sql_pzh = "select pzh,idpzh from gl_pzml ml where zt >=1 and kjqj='" + kjqj + "' and gsdm = '" + LoginInfo.getInstance().Hsdwdm + "' and exists(select pzh from gl_pznr nr where  nr.kjqj='" + kjqj + "' and nr.gsdm = '" + LoginInfo.getInstance().Hsdwdm + "' and ml.pzh = nr.pzh and ml.pzly = nr.pzly and (nr.kmdm like '1002%' or nr.kmdm like '1011%') ) order by pzh";
            DataSet ds = Dao.getSql(sql_pzh, "gl_pzml", out result);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            { 
                foreach (DataRow row in ds.Tables[0].Rows) {
                    String pzh = row["pzh"].ToString();
                    String idpzh = row["idpzh"].ToString();
                    Pzxx pzxx = new Pzxx();
                    pzxx.Pzh = pzh;
                    pzxx.Idpzh = idpzh;
                    this.List_pzxx.Add(pzxx);
                }
            }
            this.comb_pzh.ItemsSource = null;
            this.comb_pzh.ItemsSource  = this.list_pzxx; 
        }

        private void comb_kjqj_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
            getPzh();
        }

        private void btn_select_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
